/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DayEvent.java
 *
 * Created on Oct 17, 2011, 9:55:31 AM
 */
package form;

import engine.Game;
import engine.Constant;
import engine.CommandCombat;
import engine.CommandDeployment;
import engine.CommandSpecial;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import xml.XMLController;

/**
 *
 * @author bi
 */
public class DayEvent extends JFrame {

    private XMLController docFaction;
    private XMLController docPlayer;
    private XMLController docSquad;
    private XMLController docDeployment;
    private XMLController docWeapon;
    private XMLController docEquipment;
    private XMLController docSpecial;
    private Vector<Constant> vCommand;
    private Game game;
    private JFrame parent;
    private CommandSpecial commandSpecial;
    private CommandDeployment commandDeployment;
    private CommandCombat commandCombat;

    /** Creates new form DayEvent */
    public DayEvent(JFrame parent, int tab) {
        initComponents();
        this.setLocation(300, 50);

        this.parent = parent;

        pnlTabMain.setSelectedIndex(tab);

        docFaction = new XMLController("factions");
        docPlayer = new XMLController("players");
        docSquad = new XMLController("squads");
        docDeployment = new XMLController("deployments");
        docWeapon = new XMLController("weapons");
        docEquipment = new XMLController("equipments");
        docSpecial = new XMLController("specials");

        game = new Game(docPlayer, docFaction, docSquad, docDeployment, docWeapon, docEquipment, docSpecial);
        commandSpecial = new CommandSpecial(pnlBeginOrder, docPlayer, docFaction, docDeployment, docSpecial);
        commandDeployment = new CommandDeployment(pnlDeploymentOrder, docPlayer, docSquad);
        commandCombat = new CommandCombat(pnlCombatOrder, docPlayer, docFaction);

        initComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlTitle = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pnlWeather = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        cbxWeather = new javax.swing.JComboBox();
        pnlTabMain = new javax.swing.JTabbedPane();
        pnlDeployment = new javax.swing.JPanel();
        pnlDeploymentMain = new javax.swing.JPanel();
        jSplitPane3 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        pnlDeploymentOrder = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        txtDeployment = new javax.swing.JTextArea();
        pnlDeploymentButton = new javax.swing.JPanel();
        btnDeploymentDeploy = new javax.swing.JButton();
        btnDeploymentClose = new javax.swing.JButton();
        pnlBegin = new javax.swing.JPanel();
        pnlBeginMain = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtBegin = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        pnlBeginOrder = new javax.swing.JPanel();
        pnlBeginButton = new javax.swing.JPanel();
        btnBeginPlan = new javax.swing.JButton();
        btnBeginClose = new javax.swing.JButton();
        pnlCombat = new javax.swing.JPanel();
        pnlCombatMain = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        scrCombatText = new javax.swing.JScrollPane();
        txtCombat = new javax.swing.JTextArea();
        scrCombatOrder = new javax.swing.JScrollPane();
        pnlCombatOrder = new javax.swing.JPanel();
        pnlCombatButton = new javax.swing.JPanel();
        btnCombatInit = new javax.swing.JButton();
        btnCombatClose = new javax.swing.JButton();
        pnlEnd = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        chkEndApplySkill = new javax.swing.JCheckBox();
        chkEndApplyResource = new javax.swing.JCheckBox();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtEnd = new javax.swing.JTextArea();
        pnlEndButton = new javax.swing.JPanel();
        btnEndGenerate = new javax.swing.JButton();
        btnEndClose = new javax.swing.JButton();
        pnlStatus = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        txtStatus = new javax.swing.JTextArea();
        pnlStatusButton = new javax.swing.JPanel();
        btnStatusGenerate = new javax.swing.JButton();
        btnStatusClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Warhammer 40.000: Text Game - Day Event");
        setMinimumSize(new java.awt.Dimension(800, 600));
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        pnlTitle.setLayout(new javax.swing.BoxLayout(pnlTitle, javax.swing.BoxLayout.LINE_AXIS));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Warhammer 40.000: Text Game");
        pnlTitle.add(jLabel1);

        getContentPane().add(pnlTitle);

        pnlWeather.setMaximumSize(new java.awt.Dimension(32767, 20));
        pnlWeather.setLayout(new java.awt.GridLayout(1, 0, 20, 0));

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Weather:");
        pnlWeather.add(jLabel4);

        cbxWeather.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        pnlWeather.add(cbxWeather);

        getContentPane().add(pnlWeather);

        pnlDeployment.setLayout(new javax.swing.BoxLayout(pnlDeployment, javax.swing.BoxLayout.Y_AXIS));

        pnlDeploymentMain.setLayout(new javax.swing.BoxLayout(pnlDeploymentMain, javax.swing.BoxLayout.LINE_AXIS));

        jSplitPane3.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jScrollPane1.setViewportView(pnlDeploymentOrder);

        jSplitPane3.setLeftComponent(jScrollPane1);

        txtDeployment.setColumns(20);
        txtDeployment.setRows(5);
        jScrollPane6.setViewportView(txtDeployment);

        jSplitPane3.setRightComponent(jScrollPane6);

        pnlDeploymentMain.add(jSplitPane3);

        pnlDeployment.add(pnlDeploymentMain);

        pnlDeploymentButton.setLayout(new javax.swing.BoxLayout(pnlDeploymentButton, javax.swing.BoxLayout.LINE_AXIS));

        btnDeploymentDeploy.setText("Deploy");
        btnDeploymentDeploy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeploymentDeployActionPerformed(evt);
            }
        });
        pnlDeploymentButton.add(btnDeploymentDeploy);

        btnDeploymentClose.setText("Close");
        btnDeploymentClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeploymentCloseActionPerformed(evt);
            }
        });
        pnlDeploymentButton.add(btnDeploymentClose);

        pnlDeployment.add(pnlDeploymentButton);

        pnlTabMain.addTab("Phase 1 - Deployment", pnlDeployment);

        pnlBegin.setLayout(new javax.swing.BoxLayout(pnlBegin, javax.swing.BoxLayout.Y_AXIS));

        pnlBeginMain.setLayout(new javax.swing.BoxLayout(pnlBeginMain, javax.swing.BoxLayout.LINE_AXIS));

        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        txtBegin.setColumns(20);
        txtBegin.setRows(5);
        jScrollPane2.setViewportView(txtBegin);

        jSplitPane2.setRightComponent(jScrollPane2);

        jScrollPane3.setViewportView(pnlBeginOrder);

        jSplitPane2.setLeftComponent(jScrollPane3);

        pnlBeginMain.add(jSplitPane2);

        pnlBegin.add(pnlBeginMain);

        pnlBeginButton.setLayout(new javax.swing.BoxLayout(pnlBeginButton, javax.swing.BoxLayout.LINE_AXIS));

        btnBeginPlan.setText("Plan");
        btnBeginPlan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBeginPlanActionPerformed(evt);
            }
        });
        pnlBeginButton.add(btnBeginPlan);

        btnBeginClose.setText("Close");
        btnBeginClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBeginCloseActionPerformed(evt);
            }
        });
        pnlBeginButton.add(btnBeginClose);

        pnlBegin.add(pnlBeginButton);

        pnlTabMain.addTab("Phase 2 - Begin Day", pnlBegin);

        pnlCombat.setLayout(new javax.swing.BoxLayout(pnlCombat, javax.swing.BoxLayout.Y_AXIS));

        pnlCombatMain.setLayout(new javax.swing.BoxLayout(pnlCombatMain, javax.swing.BoxLayout.LINE_AXIS));

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        txtCombat.setColumns(20);
        txtCombat.setEditable(false);
        txtCombat.setRows(5);
        scrCombatText.setViewportView(txtCombat);

        jSplitPane1.setRightComponent(scrCombatText);

        pnlCombatOrder.setLayout(new java.awt.GridLayout(1, 2, 40, 40));
        scrCombatOrder.setViewportView(pnlCombatOrder);

        jSplitPane1.setLeftComponent(scrCombatOrder);

        pnlCombatMain.add(jSplitPane1);

        pnlCombat.add(pnlCombatMain);

        pnlCombatButton.setMinimumSize(new java.awt.Dimension(238, 23));
        pnlCombatButton.setPreferredSize(new java.awt.Dimension(238, 23));
        pnlCombatButton.setLayout(new javax.swing.BoxLayout(pnlCombatButton, javax.swing.BoxLayout.LINE_AXIS));

        btnCombatInit.setText("Init Battle");
        btnCombatInit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCombatInitActionPerformed(evt);
            }
        });
        pnlCombatButton.add(btnCombatInit);

        btnCombatClose.setText("Close");
        btnCombatClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCombatCloseActionPerformed(evt);
            }
        });
        pnlCombatButton.add(btnCombatClose);

        pnlCombat.add(pnlCombatButton);

        pnlTabMain.addTab("Phase 3 - Combat", pnlCombat);

        pnlEnd.setLayout(new javax.swing.BoxLayout(pnlEnd, javax.swing.BoxLayout.Y_AXIS));

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        chkEndApplySkill.setSelected(true);
        chkEndApplySkill.setText("Apply Skill");
        jPanel1.add(chkEndApplySkill);

        chkEndApplyResource.setSelected(true);
        chkEndApplyResource.setText("Apply Resource");
        jPanel1.add(chkEndApplyResource);

        pnlEnd.add(jPanel1);

        txtEnd.setColumns(20);
        txtEnd.setEditable(false);
        txtEnd.setRows(5);
        jScrollPane4.setViewportView(txtEnd);

        pnlEnd.add(jScrollPane4);

        pnlEndButton.setLayout(new javax.swing.BoxLayout(pnlEndButton, javax.swing.BoxLayout.LINE_AXIS));

        btnEndGenerate.setText("Generate Report");
        btnEndGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEndGenerateActionPerformed(evt);
            }
        });
        pnlEndButton.add(btnEndGenerate);

        btnEndClose.setText("Close");
        btnEndClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEndCloseActionPerformed(evt);
            }
        });
        pnlEndButton.add(btnEndClose);

        pnlEnd.add(pnlEndButton);

        pnlTabMain.addTab("Phase 4 - End Day", pnlEnd);

        pnlStatus.setLayout(new javax.swing.BoxLayout(pnlStatus, javax.swing.BoxLayout.Y_AXIS));

        txtStatus.setColumns(20);
        txtStatus.setEditable(false);
        txtStatus.setRows(5);
        jScrollPane5.setViewportView(txtStatus);

        pnlStatus.add(jScrollPane5);

        pnlStatusButton.setLayout(new javax.swing.BoxLayout(pnlStatusButton, javax.swing.BoxLayout.LINE_AXIS));

        btnStatusGenerate.setText("Generate Status");
        btnStatusGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStatusGenerateActionPerformed(evt);
            }
        });
        pnlStatusButton.add(btnStatusGenerate);

        btnStatusClose.setText("Close");
        btnStatusClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStatusCloseActionPerformed(evt);
            }
        });
        pnlStatusButton.add(btnStatusClose);

        pnlStatus.add(pnlStatusButton);

        pnlTabMain.addTab("Phase 5 - Status", pnlStatus);

        getContentPane().add(pnlTabMain);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStatusCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStatusCloseActionPerformed
        this.dispose();
        parent.setVisible(true);
}//GEN-LAST:event_btnStatusCloseActionPerformed

    private void btnStatusGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStatusGenerateActionPerformed
        game.startStatus();

        txtStatus.setText(game.getResultText());
}//GEN-LAST:event_btnStatusGenerateActionPerformed

    private void btnEndCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEndCloseActionPerformed
        this.dispose();
        parent.setVisible(true);
}//GEN-LAST:event_btnEndCloseActionPerformed

    private void btnEndGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEndGenerateActionPerformed
        game.startReport(chkEndApplySkill.isSelected(), chkEndApplyResource.isSelected(), cbxWeather.getSelectedItem().toString());
        if (chkEndApplySkill.isSelected()) {
            docDeployment.table = game.getDeploymentResult();
            docDeployment.writeXML();
        }

        docPlayer.table = game.getPlayerResult();
        docPlayer.writeXML();
        txtEnd.setText(game.getResultText());
}//GEN-LAST:event_btnEndGenerateActionPerformed

    private void btnCombatCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCombatCloseActionPerformed
        this.dispose();
        parent.setVisible(true);
}//GEN-LAST:event_btnCombatCloseActionPerformed

    private void btnCombatInitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCombatInitActionPerformed
        boolean result;
        result = game.startCombat(commandCombat.getCommand(), cbxWeather.getSelectedItem().toString());
        txtCombat.setText(game.getResultText());

        docDeployment.table = game.getDeploymentResult();
        docDeployment.writeXML();
}//GEN-LAST:event_btnCombatInitActionPerformed

    private void btnBeginCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBeginCloseActionPerformed
        this.dispose();
        parent.setVisible(true);
}//GEN-LAST:event_btnBeginCloseActionPerformed

    private void btnBeginPlanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBeginPlanActionPerformed
        game.startSpecial(commandSpecial.getCommand());
        txtBegin.setText(game.getResultText());

        docDeployment.table = game.getDeploymentResult();
        docDeployment.writeXML();
        docPlayer.table = game.getPlayerResult();
        docPlayer.writeXML();
}//GEN-LAST:event_btnBeginPlanActionPerformed

    private void btnDeploymentCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeploymentCloseActionPerformed
        this.dispose();
        parent.setVisible(true);
}//GEN-LAST:event_btnDeploymentCloseActionPerformed

    private void btnDeploymentDeployActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeploymentDeployActionPerformed
        boolean result = game.startDeploy(commandDeployment.getCommand());

        if (!result) {
            JOptionPane.showMessageDialog(this, "Error while deploy","Deploy",JOptionPane.ERROR_MESSAGE);
        }

        txtDeployment.setText(game.getResultText());
        docDeployment.table = game.getDeploymentResult();
        docDeployment.writeXML();
        docPlayer.table = game.getPlayerResult();
        docPlayer.writeXML();
}//GEN-LAST:event_btnDeploymentDeployActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBeginClose;
    private javax.swing.JButton btnBeginPlan;
    private javax.swing.JButton btnCombatClose;
    private javax.swing.JButton btnCombatInit;
    private javax.swing.JButton btnDeploymentClose;
    private javax.swing.JButton btnDeploymentDeploy;
    private javax.swing.JButton btnEndClose;
    private javax.swing.JButton btnEndGenerate;
    private javax.swing.JButton btnStatusClose;
    private javax.swing.JButton btnStatusGenerate;
    private javax.swing.JComboBox cbxWeather;
    private javax.swing.JCheckBox chkEndApplyResource;
    private javax.swing.JCheckBox chkEndApplySkill;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JPanel pnlBegin;
    private javax.swing.JPanel pnlBeginButton;
    private javax.swing.JPanel pnlBeginMain;
    private javax.swing.JPanel pnlBeginOrder;
    private javax.swing.JPanel pnlCombat;
    private javax.swing.JPanel pnlCombatButton;
    private javax.swing.JPanel pnlCombatMain;
    private javax.swing.JPanel pnlCombatOrder;
    private javax.swing.JPanel pnlDeployment;
    private javax.swing.JPanel pnlDeploymentButton;
    private javax.swing.JPanel pnlDeploymentMain;
    private javax.swing.JPanel pnlDeploymentOrder;
    private javax.swing.JPanel pnlEnd;
    private javax.swing.JPanel pnlEndButton;
    private javax.swing.JPanel pnlStatus;
    private javax.swing.JPanel pnlStatusButton;
    private javax.swing.JTabbedPane pnlTabMain;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JPanel pnlWeather;
    private javax.swing.JScrollPane scrCombatOrder;
    private javax.swing.JScrollPane scrCombatText;
    private javax.swing.JTextArea txtBegin;
    private javax.swing.JTextArea txtCombat;
    private javax.swing.JTextArea txtDeployment;
    private javax.swing.JTextArea txtEnd;
    private javax.swing.JTextArea txtStatus;
    // End of variables declaration//GEN-END:variables

    private void initComboBox() {
        Vector<String> listWeather = new Vector<String>();
        listWeather.add("Clear Day");
        listWeather.add("Rain Storm");
        listWeather.add("Winter Storm");
        listWeather.add("Sand Storm");
        listWeather.add("Warp Storm");
        listWeather.add("Magnetic Storm");
        listWeather.add("Spore Air");

        cbxWeather.setModel(new DefaultComboBoxModel(listWeather));
    }
}
